//
//  ADSegmentViewController.m
//  MySegment
//
//  Created by Adiljan on 2016/12/6.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "ADSegmentViewController.h"
#import "ADSegmentView.h"
#import "ADSegmentTool.h"

#define phone_width [UIScreen mainScreen].bounds.size.width
#define phone_height [UIScreen mainScreen].bounds.size.height
#define nav_height 64
#define segment_x 30
#define segment_y nav_height + 20
#define segment_height 30
@interface ADSegmentViewController ()

@property (nonatomic, strong) ADSegmentView *adSegment;
@end

@implementation ADSegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSegment];
    
}

/*Adiljan 创建 SegmentView */
- (void)initSegment{
    self.adSegment = [[ADSegmentView alloc]initWithFrame:CGRectMake(segment_x, segment_y, phone_width-2*segment_x, segment_height)];
    self.adSegment.baseColor = [ADSegmentTool colorWithHexString:@"FF88BD"];
    self.adSegment.hightLightedColor = [ADSegmentTool colorWithHexString:@"D8D8D8"];
    
    [self.view addSubview:self.adSegment];
    
    __weak ADSegmentViewController *weakSelf = self;
    [self.adSegment setADSegmentClickBlock:^(int index) {
        
        weakSelf.selectedViewControllerIndex = index;
    }];
    
}

- (void)setViewController:(NSArray *)controllers{
    // 把self.arrTitles 给 segment 设置segment的title
    self.adSegment.arrTitles = self.arrTitles;
    for (int i = 0; i < [controllers count]; i++) {
        [self pushViewController:controllers[i]];
    }
    self.selectedViewControllerIndex = self.arrTitles.count-1;
}
- (void)setViewController:(NSArray *)controllers tiltes:(NSArray *)titles{
    
    // 把titles 给 segment 设置segment的title
    self.adSegment.arrTitles = titles;
    for (int i = 0; i < [controllers count]; i++) {
        [self pushViewController:controllers[i]];
    }

    self.selectedViewControllerIndex = titles.count-1;
}
/*Adiljan 添加为 self 的 子 Control*/
- (void)pushViewController:(UIViewController *)viewController{
    
    [self addChildViewController:viewController];
    
}
/*Adiljan 选择的 control*/
- (void)setSelectedViewControllerIndex:(NSInteger)index
{
    if (!_selectedViewController) {
        _selectedViewController = self.childViewControllers[index];
        
        [_selectedViewController view].frame = CGRectMake(0, segment_y+segment_height +10, self.view.frame.size.width, self.view.frame.size.height);
        
        [self.view addSubview:[_selectedViewController view]];
        [_selectedViewController didMoveToParentViewController:self];
    } else {
        
        [self.childViewControllers[index] view].frame = CGRectMake(0, segment_y +segment_height+10, self.view.frame.size.width, self.view.frame.size.height);
        
        [self transitionFromViewController:_selectedViewController toViewController:self.childViewControllers[index] duration:0.0f options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
            _selectedViewController = self.childViewControllers[index];
            _selectedViewControllerIndex = index;
        }];
    }
}
@end
